/*
 * Pratama Nur Wijaya (c) 2013 
 * 
 * Project       : Sound Of Indonesia
 * Filename      : Scene_Menu_Utama.java
 * Creation Date : Mar 30, 2013 time : 10:22:32 PM
 *
 */

package id.pratama.soundofindonesia.scene;

import id.pratama.soundofindonesia.IManageableScene;
import id.pratama.soundofindonesia.SceneManager;
import id.pratama.soundofindonesia.Utils;
import id.pratama.soundofindonesia.config.IApps_Data;
import id.pratama.soundofindonesia.config.IApps_Define;
import id.pratama.soundofindonesia.data.IGraphic_Data;
import org.andengine.entity.scene.IOnAreaTouchListener;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.BaseGameActivity;

public class Scene_Menu_Utama extends Scene implements IManageableScene, IApps_Data, IApps_Define, IGraphic_Data,
		IOnAreaTouchListener
{
	private BaseGameActivity	activity;
	private SceneManager		sceneManager;

	private BitmapTextureAtlas	texMenuBg;
	private ITextureRegion		regMenuBg;
	private Sprite				sprMenuBg;

	private BitmapTextureAtlas	texMenuFontSOI;
	private ITextureRegion		regMenuFontSOI;
	private Sprite				sprMenuFontSOI;

	private BitmapTextureAtlas	texMenuBtnPlay;
	private ITextureRegion		regMenuBtnPlay;
	private Sprite				sprMenuBtnPlay;

	private BitmapTextureAtlas	texMenuBtnCredit;
	private ITextureRegion		regMenuBtnCredit;
	private Sprite				sprMenuBtnCredit;

	public Scene_Menu_Utama(BaseGameActivity activity)
	{
		this.activity = activity;
		sceneManager = SceneManager.getInstance();
		setOnAreaTouchListener(this);
		loadResources();
		applyResources();
		registerUpdateHandler();
	}

	@Override
	public void loadResources()
	{
		if (texMenuBg == null)
			texMenuBg = new BitmapTextureAtlas(activity.getTextureManager(), MENU_UTAMA_BG_TEX_WIDTH,
					MENU_UTAMA_BG_TEX_HEIGHT, Utils.getTextureOptions());

		if (texMenuFontSOI == null)
			texMenuFontSOI = new BitmapTextureAtlas(activity.getTextureManager(), MENU_FONT_SOI_TEX_WIDTH,
					MENU_FONT_SOI_TEX_HEIGHT, Utils.getTextureOptions());

		if (texMenuBtnPlay == null)
			texMenuBtnPlay = new BitmapTextureAtlas(activity.getTextureManager(), MENU_BTN_PLAY_TEX_WIDTH,
					MENU_BTN_PLAY_TEX_HEIGHT, Utils.getTextureOptions());

		if (texMenuBtnCredit == null)
			texMenuBtnCredit = new BitmapTextureAtlas(activity.getTextureManager(), MENU_BTN_CREDIT_TEX_WIDTH,
					MENU_BTN_CREDIT_TEX_HEIGHT, Utils.getTextureOptions());

		regMenuBg = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texMenuBg, activity, MENU_UTAMA_BG, 0, 0);
		regMenuFontSOI = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texMenuFontSOI, activity,
				MENU_UTAMA_FONT_SOI, 0, 0);
		regMenuBtnPlay = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texMenuBtnPlay, activity,
				MENU_BTN_PLAY, 0, 0);
		regMenuBtnCredit = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texMenuBtnCredit, activity,
				MENU_BTN_CREDIT, 0, 0);

		texMenuBg.load();
		texMenuFontSOI.load();
		texMenuBtnPlay.load();
		texMenuBtnCredit.load();

	}

	@Override
	public void applyResources()
	{
		if (sprMenuBg == null)
			sprMenuBg = new Sprite(0, 0, RES_WIDTH, RES_HEIGHT, regMenuBg, activity.getVertexBufferObjectManager());

		if (sprMenuFontSOI == null)
			sprMenuFontSOI = new Sprite(0, 0, Utils.getRatioWidth(MENU_FONT_SOI_WIDTH),
					Utils.getRatioHeight(MENU_FONT_SOI_HEIGHT), regMenuFontSOI, activity.getVertexBufferObjectManager());

		if (sprMenuBtnPlay == null)
			sprMenuBtnPlay = new Sprite(0, 0, Utils.getRatioWidth(MENU_BTN_PLAY_WIDTH),
					Utils.getRatioHeight(MENU_BTN_PLAY_HEIGHT), regMenuBtnPlay, activity.getVertexBufferObjectManager());

		if (sprMenuBtnCredit == null)
			sprMenuBtnCredit = new Sprite(0, 0, Utils.getRatioWidth(MENU_BTN_CREDIT_WIDTH),
					Utils.getRatioHeight(MENU_BTN_CREDIT_HEIGHT), regMenuBtnCredit,
					activity.getVertexBufferObjectManager());

		sprMenuFontSOI.setPosition(RES_WIDTH / 2 - sprMenuFontSOI.getWidth() / 2, Utils.getRatio(40));
		sprMenuBtnPlay.setPosition(Utils.getRatio(230), Utils.getRatio(200));
		sprMenuBtnCredit.setPosition(RES_WIDTH - sprMenuBtnCredit.getWidth() - Utils.getRatio(10), RES_HEIGHT
				- sprMenuBtnCredit.getHeight() - Utils.getRatio(10));

		attachChild(sprMenuBg);
		sprMenuBg.attachChild(sprMenuFontSOI);
		sprMenuBg.attachChild(sprMenuBtnPlay);
		sprMenuBg.attachChild(sprMenuBtnCredit);

		this.registerTouchArea(sprMenuBtnPlay);
		this.registerTouchArea(sprMenuBtnCredit);

	}

	@Override
	public void registerUpdateHandler()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void unloadResources()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX,
			float pTouchAreaLocalY)
	{
		if (SceneManager.currentScene == SceneManager.SCENE_MENU_UTAMA)
		{
			if (pTouchArea.equals(sprMenuBtnPlay))
			{
				sceneManager.setScene(SceneManager.SCENE_MENU_PILIH_ALAT_MUSIK);
			} else if (pTouchArea.equals(sprMenuBtnCredit))
			{
				Utils.TRACE("Pencet credit");
				sceneManager.setScene(SceneManager.SCENE_MENU_CREDIT);
			}
		}
		return false;
	}

}
